package com.sangmo.boot.auth.core.model;


import com.sangmo.boot.framework.data.entity.general.AuditEntity;
import com.sangmo.boot.framework.data.entity.general.LongEntity2;
import com.sangmo.boot.framework.data.entity.guid.GuidAuditEntity;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.Entity;
import javax.persistence.Table;
import java.beans.Transient;
import java.util.Date;

@Entity
@Table(name = "access_token")
public class AccessToken extends LongEntity2 {

	private String username;
	private String clientId;
	private String value;
	private String type;
	private long validity;
	private String scope;
	private String resource;
	private Date refreshTime;

	public String getResource() {
		return resource;
	}

	public void setResource(String resource) {
		this.resource = resource;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getClientId() {
		return clientId;
	}
	public void setClientId(String clientId) {
		this.clientId = clientId;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}

	public long getValidity() {
		return validity;
	}

	public void setValidity(long validity) {
		this.validity = validity;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public Date getRefreshTime() {
		return refreshTime;
	}

	public void setRefreshTime(Date refreshTime) {
		this.refreshTime = refreshTime;
	}

	@Transient
	public long getLeftValidity() {
		long now = System.currentTimeMillis();
		long time = refreshTime.getTime() + (validity * 1000);
		long left = (time - now) / 1000;
		return left > 0 ? left : 0;
	}

	@Transient
	public boolean isExpired() {
		long now = System.currentTimeMillis();
		if(-1 == this.validity) {
			return false;
		}
		long time = refreshTime.getTime() + (validity * 1000);
		return now >= time;
	}

	public boolean containsResource(String resource) {
		if(StringUtils.isEmpty(resource)) {
			return true;
		}
		if(StringUtils.isEmpty(this.resource)) {
			return false;
		}
		String[] clientResources = this.resource.split(",", -1);
		String[] resourceArr = resource.split(",", -1);

		for (String s : resourceArr) {
			if (!ArrayUtils.contains(clientResources, s)) {
				return false;
			}
		}
		return true;
	}

	public boolean containsScope(String scope) {
		if(StringUtils.isEmpty(scope)) {
			return true;
		}
		if(StringUtils.isEmpty(this.scope)) {
			return false;
		}

		String[] clientScopes = this.getScope().split(",", -1);
		String[] scopeArr = scope.split(",", -1);
		for (String s : scopeArr) {
			if (!ArrayUtils.contains(clientScopes, s)) {
				return false;
			}
		}
		return true;
	}

	public interface Types {
		String BEARER = "BEARER";
	}
}
